﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MyChat.Model
{
    public class ModbusTCPModel
    {
        private Socket modbusSocket;
        public ModbusTCPModel()
        {
            modbusSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        // 连接到 Modbus 服务器
        public bool Connect(string ipAddress, int port)
        {
            try
            {
                modbusSocket.Connect(ipAddress, port);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("连接失败: " + ex.Message);
                return false;
            }
        }
        // 关闭连接
        public void Disconnect()
        {
            if (modbusSocket.Connected)
            {
                modbusSocket.Close();
            }
        }
    }
}
